package com.sunmnet.j2ee.action.powerdb.designer;

import com.sunmnet.j2ee.action.BaseAction;
import com.sunmnet.j2ee.action.BaseNewAction;
import com.sunmnet.j2ee.bean.pub.action.JsonResult;
import com.sunmnet.j2ee.bean.pub.action.PageResult;
import com.sunmnet.j2ee.core.pub.QueryFilter;
import com.sunmnet.j2ee.core.pub.annotation.MethodName;
import com.sunmnet.j2ee.entity.powerdb.designer.AccountManage;
import com.sunmnet.j2ee.entity.powerdb.designer.Database;
import com.sunmnet.j2ee.entity.security.Account;
import com.sunmnet.j2ee.service.BaseService;
import com.sunmnet.j2ee.service.powerdb.designer.AccountManageService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.dom4j.Document;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Controller("/powerdb/designer/accountManage")
@RequestMapping("/powerdb/designer/accountManage")
public class AccountManageAction {

	private static final Logger LOG = LogManager.getLogger(AccountManageAction.class.getName());

	@Resource(name = "/powerdb/designer/accountManageService")
	private AccountManageService service;

	/**
	 * 批量查询
	 *
	 * @return JsonResult
	 */

	@MethodName(name = "保存项目与账户关系")
	@RequestMapping(value = "/saveAccountRelProject", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult saveAccountRelProject(@RequestParam(value = "id", required = true) Long accountId,
	                                        @RequestParam(value = "ids", required = true) String projectIds,
	                                        short right) {

		JsonResult json = new JsonResult(true);
		json.setMsg("保存项目与账户关系");
		try {
			service.deleteAccountRleProject(accountId, projectIds);
			json.setObj(service.saveAccountRelProject(accountId, projectIds, right));
		} catch (Exception e) {
			BaseAction.setErrMsg("保存项目与账户关系失败", LOG, json, e);
		}
		return json;
	}

	@MethodName(name = "查询用户与数据库连接关系")
	@RequestMapping(value = "/getDbRelAccount", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult getAccountDb(@RequestParam(value = "id", required = true) Long id) {
		JsonResult json = new JsonResult();
		json.setObj(service.getAccountDb(id));
		return json;
	}

	@MethodName(name = "查询用户与项目的权限关系")
	@RequestMapping(value = "/getAccountProject", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult getAccountProject(@RequestParam(value = "id", required = true) Long id) {
		JsonResult json = new JsonResult();
		try {
			json.setObj(service.getAccountProject(id));
		} catch (Exception e) {
			BaseAction.setErrMsg("查询用户与项目的权限关系", LOG, json, e);
		}
		return json;
	}

	@MethodName(name = "保存账户与数据库关系")
	@RequestMapping(value = "/saveAccountRelDbConnInfo", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult saveAccountRelDbConnInfo(@RequestParam(value = "id", required = true) Long accountId,
	                                           @RequestParam(value = "ids", required = true) String dbConnInfoIds) {

		JsonResult json = new JsonResult(true);
		json.setMsg("保存账户与数据库关系");
		try {
			service.deleteAccountRelDbConnInfo(accountId, dbConnInfoIds);
			json.setObj(service.saveAccountRelDbConnInfo(accountId, dbConnInfoIds));
		} catch (Exception e) {
			BaseAction.setErrMsg("保存账户与数据库关系失败", LOG, json, e);
		}
		return json;
	}

	@MethodName(name = "删除项目和账户关系")
	@RequestMapping(value = "/deleteAccountRleProject", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult deleteAccountRleProject(@RequestParam(value = "id", required = true) Long accountId,
	                                          @RequestParam(value = "ids", required = true) String projectIds) {

		JsonResult json = new JsonResult(true);
		json.setMsg("删除项目和账户关系成功");
		try {
			json.setObj(service.deleteAccountRleProject(accountId, projectIds));
		} catch (Exception e) {
			BaseAction.setErrMsg("删除项目和账户关系失败", LOG, json, e);
		}
		return json;
	}

	@MethodName(name = "删除数据库和账户关系")
	@RequestMapping(value = "/deleteAccountRelDbConnInfo", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult deleteAccountRelDbConnInfo(@RequestParam(value = "id", required = true) Long accountId,
	                                             @RequestParam(value = "ids", required = true) String dbConnInfoIds) {

		JsonResult json = new JsonResult(true);
		json.setMsg("删除数据库和账户关系成功");
		try {
			json.setObj(service.deleteAccountRelDbConnInfo(accountId, dbConnInfoIds));
		} catch (Exception e) {
			BaseAction.setErrMsg("删除数据库和账户关系失败", LOG, json, e);
		}
		return json;
	}

	@MethodName(name = "查询所有用户")
	@RequestMapping(value = "/findAllAccount", method = RequestMethod.PATCH)
	@ResponseBody
	public JsonResult findAllAccount() {

		JsonResult json = new JsonResult(true);
		try {
			json.setMsg("查询所有用户成功");
			json.setObj(service.findAllAccount());
		} catch (Exception e) {
			BaseAction.setErrMsg("查询所有用户失败", LOG, json, e);
		}
		return json;
	}

	@MethodName(name = "批量查询")
	@RequestMapping(value = "/findByAllSearch", method = RequestMethod.PATCH)
	@ResponseBody
	public JsonResult findByAllSearch(HttpServletRequest request) {
		QueryFilter filter = new QueryFilter(request);
		JsonResult json = new JsonResult(true);
		try {
			json.setMsg("查询所有用户成功");
			json.setObj(service.findByAllSearch(filter));
		} catch (Exception e) {
			BaseAction.setErrMsg("查询所有用户失败", LOG, json, e);
		}
		return json;
	}

}
